package com.lianrui.app.order.dao;


import java.util.List;

import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.lianrui.app.order.domain.Order;
import com.lianrui.app.order.domain.OrderItem;
import com.lianrui.app.order.domain.OrderLog;
import com.lianrui.app.order.vos.OrderPage;
import com.lianrui.commons.exception.BusinessException;
import com.lianrui.core.IDao;

@Repository("orderDao")
public interface IOrderDao extends IDao<OrderPage, Order, Integer> {
	
	@Override
	@ResultMap("orderMap")
	@Select("select * from es_order where order_id = #{id}")
	public Order unique(Integer id) throws BusinessException;
	
	/**
	 * 功能描述：添加订单日志
	 * @param log 日志对象
	 * @throws BusinessException
	 */
	@Select("insert into es_order_log (order_id, op_id, op_name, message, op_time)"
			+ " values (#{order_id}, #{op_id}, #{op_name}, #{message}, #{op_time});")
	public void addLog(OrderLog log) throws BusinessException;
	
	/**
	 * 功能说明：添加订单项
	 * @param item
	 * @throws BusinessException
	 */
	public void addItem(OrderItem... item) throws BusinessException;
	
	/**
	 * 功能说明：查询订单项
	 * @param order_id 订单编码
	 * @throws BusinessException
	 */
	@ResultType(OrderItem.class)
	@Select("select * from es_order_items where order_id = #{order_id}")
	public List<OrderItem> getItems(Integer order_id) throws BusinessException;
	
	/**
	 * 功能说明：手机端简易支付成功
	 * @param id 订单编码
	 */
	@Update("update es_order set pay_status=1, status=1, paymoney=order_amount-discount, complete_time=unix_timestamp(NOW())"
			+ " where order_id=#{id}")
	void payMobile(Integer id) throws BusinessException;
	
	@Override
	@ResultMap("orderEntity")
	@Select("select * from es_order where order_id = #{id}")
	public Order pure(Integer id) throws BusinessException;

	/**
	 * 功能说明：订单状态更新
	 * @param order 订单对象
	 */
	public void updateStatus(Order order) throws BusinessException;
}
